package com.example.club_mvvm.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.viewModelFactory
import com.example.club_mvvm.base.BaseViewModel
import com.example.club_mvvm.base.RequestEvent
import com.example.club_mvvm.http.VideoEntity
import com.example.club_mvvm.http_hilt.api.model.UpdateModel
import com.example.club_mvvm.http_hilt.mall_api.model.MallAdInfoModel
import com.example.club_mvvm.respository.MainRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import okhttp3.RequestBody
import javax.inject.Inject

/**
 * @author yaotianxue
 * @description
 * @date 2025/1/11
 */
@HiltViewModel
class MainViewModel @Inject constructor(private val repository:MainRepository) :BaseViewModel() {//使用@Inject注入repository

    //方案1
    val data1 by lazy{ MutableLiveData<MutableList<VideoEntity>>() }
    //方案2
    private val _data2 = MutableStateFlow<RequestEvent>(RequestEvent.Default)
    val data2:StateFlow<RequestEvent> = _data2.asStateFlow()
    //方案3
    private val _data3 = MutableStateFlow<RequestEvent>(RequestEvent.Default)
    val data3:StateFlow<RequestEvent> = _data3.asStateFlow()
    //判断版本是否需要更新
    val appUpdateLiveData by lazy{ MutableLiveData<UpdateModel>()}
    fun appLevelCheck() {
        viewModelScope.launch {
            repository.appLevelCheck().collect{
                if(it.code === 0){
                    appUpdateLiveData.value = it.data
                }
            }
        }
    }

    //广告数据
    val adInfoLiveData by lazy{ MutableLiveData<MutableList<MallAdInfoModel>>()}
    fun cmallAdInfo(body: RequestBody){
        viewModelScope.launch {
            repository.cmallAdInfo(body).collectLatest {
                if(it.code  === "SUCCESS"){
                    adInfoLiveData.value = it.mallAdInfo
                }
            }
        }
    }

    //方案1：使用LiveData
    fun getVideoData(params:Map<String,String>){
        viewModelScope.launch {
            repository.getVideoData(params).collect{
                if(it.code == 200){
                    data1.value = it.data
                }
            }
        }

    }
    //方案2:使用StateFlow
    fun getVideoData2(params:Map<String,String>){
        viewModelScope.launch {
            _data2.value = RequestEvent.Loading
            repository.getVideoData(params)
                .catch { err->
                    _data2.value = RequestEvent.Failed(err.message.toString())
                }.collect{
                    if(it.code == 200){
                        _data2.value = RequestEvent.Success
                    }else{
                        _data2.value = RequestEvent.Failed(it.message)
                    }
            }
        }
    }
    //方案3:使用StateFlow,高阶函数封装
    fun getVideoData3(params:Map<String,String>){
        execute(
            action = repository.getVideoData(params),
            onStart =  {
                _data3.value = RequestEvent.Loading
            },
            onError = {
                _data3.value = RequestEvent.Failed(it)
            },
            onFinished = {
                if(it.data.isNullOrEmpty()){
                    _data3.value = RequestEvent.Empty
                }else{
                    _data3.value = RequestEvent.Success
                }
            }
        )
    }



}